﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace DevFx.FrameWork
{
    public static  class DataSetConvert
    {
        /// <summary>
        /// 将数据集强类型化
        /// </summary>
        /// <typeparam name="T">转换类型</typeparam>
        /// <param name="dataSet">数据源</param>
        /// <param name="tableIndex">需要转换表的索引</param>
        /// <returns>泛型集合</returns>
        public static IList<T> ToList<T>(this DataSet dataSet, int tableIndex)
        {
            //确认参数有效
            if (dataSet == null || dataSet.Tables.Count <= 0 || tableIndex < 0)
                return null;

            DataTable dt = dataSet.Tables[tableIndex];


            IList<T> list = new List<T>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                //创建泛型对象
                T _t = Activator.CreateInstance<T>();
                //获取对象所有属性
                PropertyInfo[] propertyInfo = _t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    foreach (PropertyInfo info in propertyInfo)
                    {
                        //属性名称和列名相同时赋值
                        if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))
                        {
                            if (dt.Rows[i][j] != DBNull.Value)
                            {
                                //info.SetValue(_t, dt.Rows[i][j].ConvertDataTo(info.PropertyType), null);
                                info.SetValue(_t, dt.Rows[i][j], null);
                            }
                            else
                            {
                                info.SetValue(_t, null, null);
                            }
                            break;
                        }
                    }
                }
                list.Add(_t);
            }
            return list;
        }
        public static  object ConvertDataTo(this object obj, Type type)
        {
            if (obj.GetType().Equals(type))
            {
                return obj;
            }
            else
            {
                try
                {
                    if (type == typeof(string)) { return obj.ToString(); }
                    MethodInfo parseMethod = null;
                    foreach (MethodInfo mi in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (mi.Name == "Parse" && mi.GetParameters().Length == 1)
                        { parseMethod = mi; break; }
                    }
                    if (parseMethod == null)
                    {
                        return null;
                    }
                    return parseMethod.Invoke(null, new object[] { obj });
                }
                catch
                {
                    return null;

                    throw;
                }
            }
        }
    }
}
